package com.justgame.project.cloud.common.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

@Component
public class OssManager {
    private final OSS oss;

    @Value("${bucket}")
    private String bucketName;

    public OssManager(OSS oss) {
        this.oss = oss;
    }

    /**
     * 上传文件到OSS
     *
     * @param objectKey OSS Object Key
     * @param file      要上传的文件
     * @throws IOException
     */
    public void putObject(String objectKey, File file) throws IOException {
        PutObjectResult result = oss.putObject(bucketName, objectKey, new FileInputStream(file));
        System.out.println("上传文件成功，ETag：" + result.getETag());
    }

    /**
     * 上传base64编码的文件到OSS
     *
     * @param objectKey    OSS Object Key
     * @param base64String base64编码的文件内容
     */
    public void putObject(String objectKey, String base64String) {
        byte[] bytes = Base64.getDecoder().decode(base64String);
        PutObjectResult result = oss.putObject(bucketName, objectKey, new ByteArrayInputStream(bytes));
        System.out.println("上传文件成功，ETag：" + result.getETag());
    }

    /**
     * 下载文件从OSS
     *
     * @param objectKey OSS Object Key
     * @param outputStream 输出流
     * @throws IOException
     */
    public void getObject(String objectKey, OutputStream outputStream) throws IOException {
        OSSObject ossObject = oss.getObject(bucketName, objectKey);
        InputStream inputStream = ossObject.getObjectContent();
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        outputStream.close();
        inputStream.close();
    }

    /**
     * 分页查询Bucket中的Object
     * @param page 页数
     * @param size 条数
     * @return Object列表
     */
    public List<OSSObjectSummary> listObjects(int page, int size) {
        int start = (page - 1) * size;
        List<OSSObjectSummary> result = new ArrayList<>();
        ObjectListing objectListing = oss.listObjects(bucketName);
        while (true) {
            List<OSSObjectSummary> objectSummaries = objectListing.getObjectSummaries();
            int end = start + size;
            if (end > objectSummaries.size()) {
                end = objectSummaries.size();
            }
            result.addAll(objectSummaries.subList(start, end));
            if (objectSummaries.size() < size) {
                break;
            }
            String lastKey = objectSummaries.get(objectSummaries.size() - 1).getKey();
            objectListing = oss.listObjects(new ListObjectsRequest(bucketName).withMarker(lastKey));
            start -= objectSummaries.size();
        }
        return result;
    }


    /**
     * 删除Bucket中的指定Object
     *
     * @param objectKey OSS Object Key
     */
    public void deleteObject(String objectKey) {
        oss.deleteObject(bucketName, objectKey);
        System.out.println("删除文件成功");
    }
}
